/*GENERAL TRAIPLER JS*/
myScroll = null;
$=jQuery;
function initIscroll(lazyContainer) {
    var _lazyContainer = jQuery("#content") || lazyContainer;
    jQuery("#content div.lazy").lazyload({
        effect: "fadeIn",
        event : "winLoaded",
        container: _lazyContainer,
        threshold: 100
    });
}

function refreshScroll() {
    if (myScroll) {
        setTimeout(function(resp) {
            myScroll.refresh();
        }, 100)
    }
}

//FastClick.attach(document.body);
// $(window).load(function() {
jQuery(document).ready(function($) {

  var $menuMobileClick = $(".menuToggle"),
      $menu = $("#megaMenu"),
      $sctPicker = $('.selectpicker'),
      $tabsBox  = $('.nav-tabs a'),
      $menuOrange = $menu.find("#orange"),
      $boxVideo = $("#watch_video"),
      $topbar = $("#traipler_topbar"),
      $closeBtn = $boxVideo.find(".close"),
      $boxSalesVd = $("#tabs_to_content a");

  initSharesCount();
  initGaButtons();
  initTopTooltip();

  $menuMobileClick.on("click", function() {
      //console.log("toggle")
      $menu.slideToggle('2000',"swing", function () {
          // Animation complete.
      });
  });
    
  var timeout = setTimeout(function() {
      jQuery("#content div.lazy").trigger("winLoaded")
  }, 500);
    
    
  //initSocialFlags();
  setTimeout(function(resp) {
      initIscroll();
  }, 300)
    
  
  //init select in form bootstrap
  $sctPicker.selectpicker({dropupAuto:false});

  //tabs bootstrap
  $tabsBox.on("click touchstart", function (e) {
      e.preventDefault();
      $(this).tab('show');
  });

  /*toggle watch_video up*/
  $menuOrange.on("click touchstart", function(){
      $boxVideo.slideToggle();
      $("html, body").animate({scrollTop: $("body").offset().top}, 1500);
      $topbar.toggleClass("switchtopbar");
      $(".header-video").toggleClass("switchHvd");
  });

  $closeBtn.on("click touchstart", function(){
      $boxVideo.slideToggle();
      $topbar.toggleClass("switchtopbar");
      $(".header-video").toggleClass("switchHvd");
      $menu.hide();
  });
  /*Fine Js per toggle watch_video*/
        

  /* ScrollTO */
  $boxSalesVd.click( function() {
      $("#tabs_to_content a").removeClass("active");
      $(this).addClass("active");
      return false;
  });

  $("[data-action=scrollto]").click(function() {
      var rel = $(this).attr("rel");
      $('html, body').animate({
        scrollTop: $(rel).offset().top - ( $(rel).parent().parent().outerHeight(true) + 20 )
      }, 1000); 
      return false;
  });


}); //END READY


function initSharesCount(){
  
  jQuery(".traipler_block_iliveitaly").each(function(index, el) {
    var that = jQuery(this);
    var permalink = jQuery(this).find(".permalink").attr("href");
    
    get_fb_likes(permalink).then(function(count){
      that.find(".post_likes .counter").text(count);
    })
  });
  
}

function initGaButtons() {
    jQuery("[data-ga]").on('click', function(e) {
        console.log($(this), $(this).data("ga"))
        ga('send', {
            'hitType': 'event', // Required.
            'eventCategory': 'button', // Required.
            'eventAction': 'click', // Required.
            'eventLabel': $(this).data("ga"),
            'eventValue': 1
        });
        //ga('send', 'event', 'button', 'click', 'nav-buttons');
    });
}
/*END VIMEO SLIDER*/


function addEvent(element, eventName, callback) {
    if (element.addEventListener) {
        element.addEventListener(eventName, callback, false);
    } else {
        element.attachEvent(eventName, callback, false);
    }
}

function ready(player_id) {
    var froogaloop = $f(player_id);
    froogaloop.addEvent('play', function(data) {
        $('.flexslider').flexslider("pause");
        $('.video_over').fadeOut();
    });
    froogaloop.addEvent('pause', function(data) {
        $('.flexslider').flexslider("play");
        $('.video_over').fadeIn();
    });
}

function getVimeoThumb(video_id) {
    $.ajax({
        type: 'GET',
        url: 'http://vimeo.com/api/v2/video/' + video_id + '.json',
        jsonp: 'callback',
        dataType: 'jsonp',
        success: function(data) {
            var thumbnail_src = data[0].thumbnail_large;
            $('body').append('<img src="' + thumbnail_src + '"/>');
        }
    });
}

//funzione che inizializza i tooltip popover
function initTopTooltip() {

      jQuery('ul #blue').popover({
          trigger: 'hover',
          'placement': 'auto',
          template: '<div class="popover right"><p>Scopri come entrare nella community</p></div>'
      });

      jQuery('ul #orange').popover({
          trigger: 'hover',
          'placement': 'bottom',
          template: '<div class="popover"><p>Scopri come realizzare il tuo videoTraipler</p></div>'
      });
      
      //Init all tooltips
      jQuery('[rel=tooltip]').tooltip({
         /* trigger: 'hover',
          'placement': 'auto',
          template: '<div class="popover">'+$(this).attr('title')+'</div>'*/
      });

}

var isMobile = function() {
    if (jQuery(window).width() < 900 && Modernizr.touch) {
        return true;
    } else {
        return false;
    }

}